//
//  LGCInforViewController.m
//  XiMaLaYa
//
//  Created by dllo on 15/11/15.
//  Copyright © 2015年 L-G. All rights reserved.
//

#import "LGCInforViewController.h"
#import <SDWebImageManager.h>
#import <UIImageView+WebCache.h>
#import "LGCTrackTableViewCell.h"
#import "LGCLastViewController.h"
#import "GetConnection.h"
#import "TrackModel.h"
#import "SongList.h"
#import <UMSocial.h>
#import <MJRefresh.h>
#import <MBProgressHUD.h>

#define HEIGHT  [[UIScreen mainScreen]bounds].size.height / 667
#define WIDTH   [[UIScreen mainScreen]bounds].size.width / 375

@interface LGCInforViewController ()<UITableViewDataSource,UITableViewDelegate>
// 播放列表
@property (nonatomic, retain) UITableView *tableVeiw;

@property (nonatomic, retain) UIScrollView *scrollView;

@property (nonatomic, retain) UILabel *labelTitle;
// 返回数组
@property (nonatomic, retain) NSMutableArray *arrayData;

@property (nonatomic, retain) NSMutableArray *arraySort;
// tableView头视图
@property (nonatomic, retain) UIScrollView *titleView;
// 透明效果图片
@property (nonatomic, retain) UIImageView *imageCover;

@property (nonatomic, retain) UIImageView *imageSmall;
// 标题图片
@property (nonatomic, retain) UIImageView *imageHeader;
// 标题
@property (nonatomic, retain) UILabel *labelHeader;

@property (nonatomic, retain) UILabel *labelTracks;

@property (nonatomic, retain) UILabel *label_title;

@property (nonatomic, retain) UILabel *label_data;

@property (nonatomic, retain) NSTimer *Mytimer;

@property (nonatomic, assign) NSInteger nextPage;

@property (nonatomic, copy) NSString *tracks;

@property (nonatomic, retain) MBProgressHUD *MBProgress;

@end
NSInteger indexNext = 0;

@implementation LGCInforViewController
- (void)dealloc
{
    [_tableVeiw release];
    [_arrayData release];
    [_arraySort release];
    [_scrollView release];
    [_labelTitle release];
    [_label_data release];
    [_label_title release];
    [_labelHeader release];
    [_labelTracks release];
    [_imageCover release];
    [_imageHeader release];
    [_imageSmall release];
    [super dealloc];
}
- (void)viewDidDisappear:(BOOL)animated
{
    indexNext = 0;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // 返回
    self.navigationController.navigationBar.translucent = YES;
    UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
    btnBack.frame = CGRectMake(0, 0, 30 * WIDTH, 30 * HEIGHT);
    [btnBack setImage:[UIImage imageNamed:@"back@2x"] forState:UIControlStateNormal];
    [btnBack addTarget:self action:@selector(btnBackAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btnBack];
    // 分享
    UIButton *btnShare = [UIButton buttonWithType:UIButtonTypeCustom];
    btnShare.frame = CGRectMake(0, 0, 30 * WIDTH, 30 * HEIGHT);
    [btnShare setImage:[UIImage imageNamed:@"share@2x"] forState:UIControlStateNormal];
    [btnShare addTarget:self action:@selector(btnShareAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btnShare];
    self.arrayData = [NSMutableArray array];
    self.arraySort = [NSMutableArray array];
    
    // tableview的头视图
    [self createTableHeader];
    
    self.tableVeiw = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    
    self.tableVeiw.showsVerticalScrollIndicator = NO;
    
    self.tableVeiw.delegate = self;
    
    self.tableVeiw.dataSource = self;
    
    self.tableVeiw.separatorStyle = 0;
        
    [self.view addSubview:self.tableVeiw];
    
    [_tableVeiw release];
    
    [self setExtraCellLineHidden:self.tableVeiw];
    
    self.tableVeiw.tableHeaderView = self.titleView;

    [self addFooter];
    // nav的效果
    [self navSubviews];
    // 时间器
    self.Mytimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(labelTitleAction) userInfo:nil repeats:YES];
}
- (void)btnShareAction:(id)sender
{
    [UMSocialSnsService presentSnsIconSheetView:self appKey:@"5654010b67e58e14dd000d95" shareText:@"" shareImage:nil shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,UMShareToEmail,nil] delegate:NULL];
}

- (void)btnBackAction:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30 * HEIGHT;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrayData.count;
}
// 自定义tableview的HeaderFooterView
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30 * HEIGHT)];
    titleView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 150 * WIDTH, 5 * HEIGHT, 150 * WIDTH, 20 * HEIGHT)];
    label.text = self.tracks;
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor grayColor];
    [titleView addSubview:label];
    [label release];
    return titleView;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LGCTrackTableViewCell *cell = [LGCTrackTableViewCell trackTableViewWith:tableView];
    cell.trackModel = self.arrayData[indexPath.row];
    return cell;
}
-(void)setExtraCellLineHidden: (UITableView *)tableView
{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
    [view release];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100 * HEIGHT;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    LGCLastViewController *last = [LGCLastViewController shareLastViewController];
    last.array_current = self.arraySort;
    last.playUrl = [self.arrayData[indexPath.row] playPathAacv224];
    last.strTitle = [self.arrayData[indexPath.row] title];
    last.indexRow = indexPath.row;
    [self.navigationController presentViewController:last animated:YES completion:^{
    }];
}
- (void)connection:(NSString *)strID and:(NSInteger)nextPage
{
    NSString *str = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/album/track/%@/true/1/%ld?device=iPhone",self.strID,self.nextPage];
    [GetConnection startGETConnection:str parmaters:nil connectionBlock:^(id responseObject) {
        [self.arrayData removeAllObjects];
        NSDictionary *dic = responseObject;
        NSDictionary *album = [dic objectForKey:@"album"];
        NSArray *tracksList = [[dic objectForKey:@"tracks"] objectForKey:@"list"];
        for (NSDictionary *temp in tracksList) {
            TrackModel *trackModel = [TrackModel trackModelWithDictionary:temp];
            [self.arrayData addObject:trackModel];
        }
        if (self.arrayData.count != 0) {
            [self.tableVeiw.mj_header endRefreshing];
            [self.tableVeiw.mj_footer endRefreshing];
        }
        self.tableVeiw.separatorStyle = 1;
        [self.tableVeiw reloadData];
        /*****************   *****************/
        for (TrackModel *track in self.arrayData) {
            SongList *songlist = [[SongList alloc] init];
            songlist.song_name = track.title;
            songlist.song_play_url = track.playPathAacv164;
            [self.arraySort addObject:songlist];
        }
     /***************** getSomeNumber *****************/
        //tableview头视图
        self.titleView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        self.labelTitle.text = [album objectForKey:@"title"];
        NSURL *urlCover = [NSURL URLWithString:[album objectForKey:@"coverLarge"]];
        [self.imageCover sd_setImageWithURL:urlCover placeholderImage:[UIImage imageNamed:@"zhanweitu"]];
        self.tracks = [NSString stringWithFormat:@"已更新%@集",[album objectForKey:@"tracks"]];
        NSURL *urlHeader = [NSURL URLWithString:[album objectForKey:@"coverWebLarge"]];
        [self.imageHeader sd_setImageWithURL:urlHeader placeholderImage:[UIImage imageNamed:@"zhanweitu"]];
        NSURL *urlSmall = [NSURL URLWithString:[album objectForKey:@"avatarPath"]];
        [self.imageSmall sd_setImageWithURL:urlSmall placeholderImage:[UIImage imageNamed:@"zhanweitu"]];
        self.labelHeader.text = [album objectForKey:@"categoryName"];
        self.label_title.text = [album objectForKey:@"title"];
        NSString * timeStampString = [NSString stringWithFormat:@"%@",[album objectForKey:@"updatedAt"]];
        NSTimeInterval _interval=[[timeStampString substringToIndex:10] doubleValue];
        NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interval];
        NSString *date_time = [NSString stringWithFormat:@"%@",date];
        self.label_data.text = [date_time substringToIndex:10];
        // 判断是否全部加载完毕
        if (self.nextPage > [[album objectForKey:@"tracks"] integerValue]) {
            [self.tableVeiw.mj_footer endRefreshingWithNoMoreData];
        }
    }];
}
#pragma mark 让scrollview偏移滑动
- (void)labelTitleAction
{
    if (indexNext > 350 * WIDTH) {
        indexNext = 0;
    }
    self.scrollView.contentOffset = CGPointMake(indexNext += 1, 0);
}
- (void)navSubviews
{
    // 创建滚动的效果
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(40 * WIDTH, 0, self.view.frame.size.width - 55 * WIDTH, 44 * HEIGHT)];
    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 1.5, 0);
    // 让scrollview不能点击
    self.scrollView.userInteractionEnabled = NO;
    self.navigationItem.titleView = self.scrollView;
    [_scrollView release];
    
    self.labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(50 * WIDTH, 0, self.scrollView.contentSize.width, 44 * HEIGHT)];
    self.labelTitle.textAlignment = NSTextAlignmentCenter;
    self.labelTitle.textColor = [UIColor blackColor];
    [self.scrollView addSubview:self.labelTitle];
    [_labelTitle release];
}
#pragma mark tableView的头视图
- (void)createTableHeader
{
    // 大view
    self.titleView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200 * HEIGHT)];
    [self.view addSubview:self.titleView];
    [_titleView release];
    
    // 大图片
    self.imageCover = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200 * HEIGHT)];
    [self.titleView addSubview:self.imageCover];
    [_imageCover release];
    // 磨砂效果
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *visualEffectView = [[UIVisualEffectView alloc]initWithEffect:blurEffect];
    [visualEffectView setFrame:CGRectMake(0, 0, self.view.frame.size.width, 200 * HEIGHT)];
    [self.titleView addSubview:visualEffectView];
    
    self.imageHeader = [[UIImageView alloc] initWithFrame:CGRectMake(30 * WIDTH, 30 * HEIGHT, 120 * WIDTH, 140 * HEIGHT)];
    [self.titleView addSubview:self.imageHeader];
    [_imageHeader release];
    
    self.imageSmall = [[UIImageView alloc] initWithFrame:CGRectMake(170 * WIDTH, 30 * HEIGHT, 60 * WIDTH, 60 * HEIGHT)];
    self.imageSmall.layer.masksToBounds = YES;
    self.imageSmall.layer.cornerRadius = 30 * HEIGHT;
    [self.titleView addSubview:self.imageSmall];
    [_imageSmall release];
    
    self.labelHeader = [[UILabel alloc] initWithFrame:CGRectMake(240 * WIDTH, 35 * HEIGHT, 90 * WIDTH, 50 * HEIGHT)];
    self.labelHeader.numberOfLines = 0;
    self.labelHeader.font = [UIFont systemFontOfSize:19];
    self.labelHeader.textColor = [UIColor whiteColor];
    [self.titleView addSubview:self.labelHeader];
    [_labelHeader release];
    
    self.label_title = [[UILabel alloc] initWithFrame:CGRectMake(170 * WIDTH, 95 * HEIGHT, 150 * WIDTH, 50 * HEIGHT)];
    self.label_title.numberOfLines = 0;
    self.label_title.textAlignment = NSTextAlignmentCenter;
    self.label_title.font = [UIFont systemFontOfSize:15];
    [self.titleView addSubview:self.label_title];
    [_label_title release];
    
    self.label_data = [[UILabel alloc] initWithFrame:CGRectMake(220 * WIDTH, 155 * HEIGHT, 100 * WIDTH, 15 * HEIGHT)];
    self.label_data.font = [UIFont systemFontOfSize:13];
    self.label_data.textAlignment = NSTextAlignmentCenter;
    [self.titleView addSubview:self.label_data];
    [_label_data release];
}
// set方法
- (void)setStrID:(NSString *)strID
{
    if (_strID != strID) {
        _strID = strID;
    }
    [self connection:strID and:self.nextPage];
}
// 刷新加载
- (void)addFooter
{
    self.tableVeiw.mj_footer = [MJRefreshAutoGifFooter footerWithRefreshingBlock:^{
        self.nextPage += 20;
        [self connection:self.strID and:self.nextPage];
    }];
}
// 创建单例
+ (instancetype)shareLGCInformation
{
    static LGCInforViewController *infor = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        infor = [[LGCInforViewController alloc] init];
    });
    return infor;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
